package com.cyk.comicforumjava.demos.service.impl;

import com.cyk.comicforumjava.demos.base.ResponseResult;
import com.cyk.comicforumjava.demos.dto.UserDto;
import com.cyk.comicforumjava.demos.enits.ChatList;
import com.cyk.comicforumjava.demos.enits.Message;
import com.cyk.comicforumjava.demos.enits.Post;
import com.cyk.comicforumjava.demos.enits.User;
import com.cyk.comicforumjava.demos.mapper.LoginMapper;
import com.cyk.comicforumjava.demos.mapper.MessageMapper;
import com.cyk.comicforumjava.demos.mapper.PostMapper;
import com.cyk.comicforumjava.demos.service.MessageService;
import com.cyk.comicforumjava.demos.utils.ThreadLocalUtil;
import com.cyk.comicforumjava.demos.vo.MessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    PostMapper postMapper;
    @Autowired
    MessageMapper messageMapper;
    @Override
    public ResponseResult postList(Long userId) {
        List<Post> postList=messageMapper.selectPostList(userId);
        List<Message>messageList=new ArrayList<>();
        for(Post post:postList){
            Message message=new Message(post);
            message.setContent("<p>"+post.getTitle()+"</p>");
            messageList.add(message);
        }
        return ResponseResult.success(messageList);
    }

    @Override
    public ResponseResult supportList(Long userId) {
        List<Message>messageList=messageMapper.selectSupportList(userId);
        return ResponseResult.success(messageList);
    }

    @Override
    public ResponseResult myBackList(Long userId) {
        List<Message>messageList=messageMapper.selectMyBackList(userId);
        return ResponseResult.success(messageList);
    }

    @Autowired
    LoginMapper loginMapper;
    @Override
    public ResponseResult getUserChat(UserDto userDto) {
        Long userId=userDto.getId();
        List<Message>chatList=messageMapper.selectUserName(userDto);
        List<User>userList=new ArrayList<>();
        List<Long>idList=new ArrayList<>();
        for(Message message:chatList){
            userList.add(loginMapper.selectByUserId(message.getSenderId()));
            idList.add(message.getSenderId());
        }
        List<ChatList>chatLists=new ArrayList<>();
        for(User user:userList){
            ChatList chatList1=new ChatList();
            chatList1.setSendId(user.getId());
            chatList1.setAvatar(user.getAvator());
            chatList1.setName(user.getUsername());
            chatList1.setId(userDto.getId());
            chatLists.add(chatList1);
        }
        for(ChatList chat:chatLists){
            System.out.println(chat.getName());
            chat.setMessages(messageMapper.selectChatList(chat));
            for(MessageVo messagevo:chat.getMessages()){
                if(messagevo.getSenderId().equals(userId)){
                    messagevo.setSent(true);
                }else{
                    messagevo.setSent(false);
                }
                if(messagevo.getContent().startsWith("http")){
                    messagevo.setType("image");
                }else{
                    messagevo.setType("text");
                }
                messagevo.setTime(messagevo.getCreateTime());
                }
        }

        return ResponseResult.success(chatLists);
    }
}
